/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.algorithm.repeatstr;

import com.hyts.algorithm.Solution;
import lombok.Setter;
import org.junit.Ignore;
import org.junit.Test;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.algorithm.repeatstr
 * @author:LiBo/Alex
 * @create-date:2019-10-28 23:39
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class TestRepeat implements Solution {


    @Override
    public void solution1() {
        //xxyy xxy
        //xyyy xyy
        //xxxy xxy
        //xxxxxyy xxy
        String str = "abababbbscca";
        StringBuilder stringBuilder = new StringBuilder();
        boolean neborEvent = false;
        Character lastElement = null;
        int localCount = 1;
        for(Character element:str.toCharArray()){
            if(lastElement == null){
                lastElement = element;
                stringBuilder.append(element);
                continue;
            }
            if(element.equals(lastElement)){
                localCount++;
                lastElement = element;
            }
            if(neborEvent || localCount <= 2){
                stringBuilder.append(element);
            }else{
                neborEvent = true;
            }
        }
    }

    @Ignore
    @Test
    @Override
    public void solution2() {
        String str = "helloo";
        //AABBCC -> AABCC
        //ABBBA -> ABBA
        //AABB -> AAB
        StringBuilder stringBuilder = new StringBuilder();
        boolean flag = false;
        int repeateCount = 1;
        Character lastElement = null;
        int repeatIntervalCount=1;
        char[] elements = str.toCharArray();
        for(int i = 0 ; i< elements.length ;i++){
            Character element = elements[i];
           // currentIndex++;
            if(lastElement == null){
                lastElement = element;
                stringBuilder.append(element);
                continue;
            }
            if(element.equals(lastElement)) {
                repeateCount++;
                if(repeateCount <= 2){
                    if(repeateCount == 2){
                        repeateCount = 1;
                        repeatIntervalCount++;
                        if(repeatIntervalCount > 2){
                            flag = true;
                            repeatIntervalCount = 1;
                        }else if(repeatIntervalCount == 2){
                            flag = false;
                        }
                    }
                    if(!flag) {
                        stringBuilder.append(element);
                    }
                }
            }else{
                stringBuilder.append(element);
                lastElement = element;
                repeateCount = 1;
            }
        }
        System.out.println(stringBuilder.toString());
    }


    @Test
    @Override
    public void solution3() {

        String str = "bscbbyzbasbww";

        char[] elements = str.toCharArray();

        int currentMaxLength = 0;

        StringBuilder stringBuilder1 = new StringBuilder();

        StringBuilder stringBuilder = new StringBuilder();

        String currentMaxStr = null;

        for(int i = 0 ;i < elements.length ;i++){

             if(stringBuilder1.length() > 0 && (stringBuilder1.indexOf(String.valueOf(elements[i]))> -1)){
                if(currentMaxStr == null || stringBuilder1.length() > currentMaxLength ) {
                    currentMaxLength = stringBuilder1.length();
                    currentMaxStr = stringBuilder1.toString();
                    stringBuilder1 = new StringBuilder(String.valueOf(elements[i]));
                }else if(stringBuilder1.length() <= currentMaxLength){
                    if(stringBuilder1.length() == currentMaxLength) {
                        stringBuilder.append(stringBuilder1.toString() + ",");
                    }
                    stringBuilder1 = new StringBuilder(String.valueOf(elements[i]));
                }
            }else{
                stringBuilder1.append(elements[i]);
            }

        }
        if(currentMaxLength < stringBuilder1.length()){
            currentMaxLength = stringBuilder1.length();
        }
        System.out.println(stringBuilder.toString());
        System.out.println(currentMaxStr);
        System.out.println(currentMaxLength);
    }

    public void test4(){

        String str = "zxczxczxcabccbaasda";

        class Node{

            @Setter
            private Character value;

            @Setter
            private Node next;


            public Node(Character value, Node next) {
                this.value = value;
                this.next = next;
            }
        }
        //头结点
        Node header = new Node(str.charAt(0),null);

        char []elements = str.toCharArray();

        StringBuilder stringBuilder = new StringBuilder();

        for(int i = 1 ; i< elements.length;i++){
            //回文判断
            if(header.value.equals(elements[i])){
                stringBuilder.append(elements[i]);
            }else{

            }
           /* for(Node current = header;current.value!= null;current = current.next){
                if(current.value.equals(elements[i])){
                }
            }*/
        }
    }

}
